package com.rayc.helloworld;

import com.rabbitmq.client.*;
import com.rayc.config.RabbitmMQClient;
import org.junit.Test;

import java.io.IOException;

public class Consumer {
   @Test
    public void consumer() throws Exception {
        //1.获取Connection
        Connection connection = RabbitmMQClient.getConnection();
        //2.创建Channel
        Channel channel = connection.createChannel();
        //3.声明队列
        channel.queueDeclare("HelloWorld",true,false,false,null);
        //4.开始监听队列Queue
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收到消息："+ new String(body,"utf-8"));
                super.handleDelivery(consumerTag, envelope, properties, body);
            }
        };
        channel.basicConsume("HelloWorld", true,consumer);
        System.out.println("消费者开始监听队列");
        System.in.read();
        channel.close();
       connection.close();
    }
}
